﻿using System;
using CMS.DAO;
using Common;

namespace CMS.admin
{
    public partial class ProductAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                
                //Utilities.BuildCategoryTree( );
                var strSQL = string.Format("exec tblCategory_GetAllByCondition '{0}'", " AND parentId=346 OR parentId=356  AND status=1 ");
                var ds = DAL.GetDataSet(strSQL);
                cboCategory.DataSource = ds;
            }
        }
        private void CopyFromSreen(tblProductEntity o)
        {
            o.CategoryId = Convert.ToInt32(cboCategory.DataValueField);
            o.ProductName = Convert.ToString(txtProductName.Text);
            o.ProductCode = Convert.ToString(txtProductCode.Text);
            o.Desciption = Convert.ToString(txtDesciption.Text);
            o.ShortTitle = Convert.ToString(txtShortTitle.Text);
            o.Image = Convert.ToString(txtAnhtin.Text);
            o.InputDate = txtInputDate.SelectedDate.Value;
            o.PriceInput = Convert.ToDecimal(txtPriceInput.Text);
            o.DiscountPrice = Convert.ToDecimal(txtDiscountPrice.Text);
            o.SalePrice = Convert.ToDecimal(txtSalePrice.Text);
            o.RealPrice = Convert.ToDecimal(txtRealPrice.Text);
            o.Promotion = Convert.ToString(txtPromotion.Text);
            o.isPromotion = txtisPromotion.Checked;
            o.IsHome = txtIsHome.Checked;
            o.IsOrder = Convert.ToInt32(txtIsOrder.Text);
            o.ProductNameOther = Convert.ToString(txtProductNameOther.Text);
            o.Quantity = Convert.ToInt32(txtQuantity.Text);
            o.frkUnitId = Convert.ToInt32(txtfrkUnitId.Text);
            o.UnitName = Convert.ToString(txtUnitName.Text);
            o.Star = txtStar.Checked;
            o.Active = txtActive.Checked;
            o.IsProductHot = txtIsProductHot.Checked;
            o.WanrantyMonth = Convert.ToInt32(txtWanrantyMonth.Text);
            o.UserId = Convert.ToInt32(txtUserId.Text);
        }
        private void ClearSreen()
        {

            txtProductName.Text = "";
            txtProductCode.Text = "";
            txtDesciption.Text = "";
            txtShortTitle.Text = "";
            txtAnhtin.Text = "";
            txtPriceInput.Text = "0";
            txtDiscountPrice.Text = "0";
            txtSalePrice.Text = "0";
            txtRealPrice.Text = "0";
            txtPromotion.Text = "";
            txtIsOrder.Text = "0";
            txtProductNameOther.Text = "";
            txtQuantity.Text = "0";
            txtfrkUnitId.Text = "0";
            txtUnitName.Text = "";
            txtWanrantyMonth.Text = "0";
            txtUserId.Text = "0";
        }
        private bool ValidateScreen()
        {
            bool result = true;
            return result;
        }
        private bool Save()
        {
            tblProductDAO dao = new tblProductDAO();
            tblProductEntity o = new tblProductEntity();
            CopyFromSreen(o);
            return dao.Save(o);
        }
        private void GoBack()
        {
            Response.Redirect("ProductList.aspx");
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateScreen())
            {
                return;
            }
            if (Save())
            {
                lblInfo.Text = "Thêm mới sản phẩm thành công";
            }
            else
            {
                lblInfo.Text = "Không thêm mới được sản phẩm";
            }
        }
        protected void btnContinue_Click(object sender, EventArgs e)
        {
            ClearSreen();
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            GoBack();
        }
        protected void btnFinish_Click(object sender, EventArgs e)
        {
            GoBack();
        }
    }
}